package com.delos.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.delos.rbac.dto.UserDTO;
import com.delos.rbac.entity.UserDO;
import com.delos.rbac.mapper.UserMapper;
import com.delos.rbac.service.IRoleService;
import com.delos.rbac.service.IUserRoleService;
import com.delos.rbac.service.IUserService;
import com.delos.rbac.vo.RolesVO;
import com.delos.rbac.vo.UserRoleVO;
import com.delos.rbac.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户服务实现类
 *
 * @author zouyaowen
 * @date 2020-06-22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements IUserService {

    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private IRoleService roleService;

    @Override
    public void addUser(UserDTO userDTO) {
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userDTO, userDO);
        //TODO 创建人数据设置
        userDO.setPassword(BCrypt.hashpw(userDO.getPassword(), BCrypt.gensalt()));
        this.baseMapper.insert(userDO);
    }

    @Override
    public void deleteUser(Long userId) {
        this.baseMapper.deleteById(userId);
    }

    @Override
    public UserVO findUser(Long userId) {
        UserDO userDO = this.baseMapper.selectById(userId);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userDO, userVO);
        userVO.setDeleted(null);
        return userVO;
    }

    @Override
    public UserVO findUser(String userName) {
        QueryWrapper<UserDO> query = new QueryWrapper<>();
        query.eq("user_name", userName);
        UserDO userDO = this.baseMapper.selectOne(query);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userDO, userVO);
        return userVO;
    }

    @Override
    public UserVO getUserRole(Long userId) {
        UserDO userDO = this.getById(userId);
        if (userDO == null) {
            return null;
        }
        List<UserRoleVO> userRoleList = userRoleService.getByUserId(userDO.getId());
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userDO, userVO);
        if (!CollectionUtils.isEmpty(userRoleList)) {
            List<Long> roleIdList = userRoleList.stream().map(UserRoleVO::getRoleId).collect(Collectors.toList());
            List<RolesVO> roleList = roleService.getByRoleId(roleIdList);
            if (!CollectionUtils.isEmpty(roleList)) {
                Set<String> roleNameSet = roleList.stream().map(RolesVO::getRoleName).collect(Collectors.toSet());
                userVO.setRoleList(roleNameSet);
            }
        }
        return userVO;
    }
}
